package trial.bishi2023;

import java.util.*;

public class KDXF {
    public static void main(String[] args) {
        int[] nums = new int[]{2, 1, 0, 1, 0};
        Arrays.sort(nums);
        System.out.println(Arrays.toString(nums));
        Map<Integer, Integer> map = new TreeMap<>(Comparator.reverseOrder());
        map.put(3, 1);
        map.put(2, 8);
        map.put(1, 5);
        for (Map.Entry<Integer, Integer> i : map.entrySet()
        ) {
            System.out.println(i.getKey());
        }
        t2();
    }

    public int qualifiedQuantity(int k, int target, int[] number) {
        int ans = 0;
        for (int i = 0; i < number.length; i++) {
            int temp = Math.abs(target - number[i]);
            if (temp == 0) {
                number[i] = 0;
            } else if (temp <= 2) {
                number[i] = 1;
            } else if (temp <= 4) {
                number[i] = 2;
            } else {
                number[i] = Integer.MAX_VALUE;
            }
        }
        Arrays.sort(number);
        for (int i = 0; i < number.length; i++) {
            if (k - number[i] >= 0) {
                ans++;
                k -= number[i];
            } else {
                break;
            }
        }
        return ans;
    }

    //4.76。。。。。
    public static void t2() {
        int sum = 0;
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), m = sc.nextInt();
        int[] nums = new int[n];
        Map<Integer, Integer> map = new TreeMap<>(Comparator.reverseOrder());
        for (int i = 0; i < n; i++) {
            nums[i] = sc.nextInt();
        }
        for (int i : nums
        ) {
            sum += i;
        }
        for (int i = 0; i < m; i++) {
            int x = sc.nextInt();
            int y = sc.nextInt();
            map.put(y, x);
        }
        Arrays.sort(nums);
        int index = 0;
        out:
        for (Map.Entry<Integer, Integer> tip : map.entrySet()) {
            int num = tip.getValue(), tag = tip.getKey();
            while (num > 0) {
                if (nums[index] >= tag) {
                    break out;
                } else {
                    sum += tag - nums[index];
                    num--;
                    index++;
                }
            }
        }
        System.out.println(sum);
    }

    public int getMinOps(int[] nums, int target, int n) {
        int[][] dp = new int[nums.length][101];
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j <= 100; j++) {
                dp[i][j] = Integer.MAX_VALUE;
            }
        }
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j <= 100; j++) {
                if (i == 0) {
                    dp[i][j] = Math.min(dp[i][j], Math.abs(nums[i] - j));
                } else {
                    for (int k = -2; k <= 2; k++) {
                        if (i + k >= 0 && i + k < nums.length) {
                            int t = j + k;
                            if (t >= 0 && t <= 100) {
                                dp[i][j] = Math.min(dp[i][j], dp[i + k][t] + Math.abs(nums[i] - j));
                            }
                        }
                    }
                }
            }
        }
        int minOps = Integer.MAX_VALUE;
        for (int i = 0; i < nums.length; i++) {
            minOps = Math.min(minOps, dp[i][target]);
        }
        return minOps <= n ? minOps : -1;
    }
}
